package me.helllp.bootman.core.factory;

import java.util.Map;

import freemarker.template.Template;
import me.helllp.bootman.common.base.BaseBean;
import me.helllp.bootman.common.utils.BeanUtil;
import me.helllp.bootman.common.utils.FileUtil;
import me.helllp.bootman.common.utils.FreeMarkerUtil;
import me.helllp.bootman.core.service.FactoryService;

public abstract class AbstractFactory extends BaseBean implements FactoryService{

	private static final long serialVersionUID = 8355823306416219305L;
	
	//	唯一标识任务的标记
	protected String identify;
	
	private String basePackage;
	
	public AbstractFactory(String identify,String basePackage){
		this.identify = identify;
		this.basePackage = basePackage;
	}

	public abstract String getIdentify();
	
	/**
	 * 获取输出文件名
	 * 
	 * @return
	 */
	public abstract String getFileName();

	/**
	 * 获取模板文件
	 * 
	 * @return
	 */
	public abstract String getTemplateFile();
	
	protected Map<String,Object> makeParams(){
		Map<String,Object> map = BeanUtil.transBean2Map(this);
		map.put("basePackage", this.basePackage);
		
		return map; 
	}
	
	@Override
	public boolean putOut(String path) throws Exception {
		
		Template template = FreeMarkerUtil.cfgInstance.getTemplate(this.getTemplateFile());
		
		FileUtil.makeDir(path);
		
		FreeMarkerUtil.output(makeParams(), path + System.getProperty("file.separator") + this.getFileName(),template);
		
		return true;
	}
}
